import 'package:easy_chat_app/commons/GlobalStatus.dart';
import 'package:easy_chat_app/commons/db/repository/LocalConversationRepository.dart';
import 'package:easy_chat_app/commons/model/AppUserDetail.dart';
import 'package:easy_chat_app/commons/model/GetContactsUserInfoResultData.dart';
import 'package:easy_chat_app/commons/utils/RouteUtils.dart';
import 'package:easy_chat_app/commons/widget/UserHeadPortrait.dart';
import 'package:easy_chat_app/mobile/constants/MobileAppGlobalConstants.dart';
import 'package:easy_chat_app/mobile/page/MobileChatPage.dart';
import 'package:flutter/material.dart';
import 'package:logger/logger.dart';
import 'package:provider/provider.dart';

class MobileContactsInfoPageParams {
  GetContactsUserInfoResultData contactsUserInfo;
  LocalConversation localConversation;
  Type prePageType;

  MobileContactsInfoPageParams.create(
      {required this.contactsUserInfo,
      required this.localConversation,
      required this.prePageType});
}

class MobileContactsInfoPageChangeNotifier extends ChangeNotifier {}

class MobileContactsInfoPage extends StatelessWidget {
  MobileContactsInfoPage({super.key});

  final Logger logger = Logger();

  @override
  Widget build(BuildContext context) {
    MobileContactsInfoPageParams pageParams = RouteUtils.currentRouteSettings()
        .arguments as MobileContactsInfoPageParams;
    GetContactsUserInfoResultData contactsUserInfo =
        pageParams.contactsUserInfo;
    return Consumer<AppUserDetailChangeNotifier>(
        builder: (context, appUserDetailChangeNotifier, child) {
      AppUserDetail appUserDetail =
          appUserDetailChangeNotifier.getAppUserDetail();
      return MultiProvider(
        providers: [
          ChangeNotifierProvider(create: (context) {
            return MobileContactsInfoPageChangeNotifier();
          })
        ],
        builder: (context, child) {
          return Scaffold(
            appBar: AppBar(
              surfaceTintColor: MobileAppGlobalConstants.appBarSurfaceTintColor,
              backgroundColor: MobileAppGlobalConstants.appBarBackgroundColor,
              actions: [
                IconButton(
                    onPressed: () async {
                      await RouteUtils.route2MobileContactsInfoSettingPage(
                          contactsUserInfo);
                    },
                    icon: const Icon(Icons.more_horiz))
              ],
            ),
            body: Container(
              color: const Color.fromRGBO(237, 237, 237, 1),
              child: Column(
                children: [
                  Container(
                    color: Colors.white,
                    height: 35,
                  ),
                  Container(
                    color: Colors.white,
                    child: Row(
                      children: [
                        const SizedBox(
                          width: 25,
                        ),
                        UserHeadPortrait.network(
                            size: 65,
                            userCode: contactsUserInfo.code!,
                            userHeadPortrait: contactsUserInfo.headPortrait!,
                            userHeadPortraitStoreType:
                                contactsUserInfo.headPortraitStoreType!),
                        const SizedBox(
                          width: 20,
                        ),
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: [
                            Text(
                              contactsUserInfo.nickname ?? "",
                              style: const TextStyle(
                                  fontSize: 20, color: Colors.black),
                            ),
                            Text(
                              "EC号: ${contactsUserInfo.code ?? ""}",
                              style: const TextStyle(
                                  fontSize: 14,
                                  color: Color.fromRGBO(115, 115, 115, 1)),
                            )
                          ],
                        )
                      ],
                    ),
                  ),
                  Container(
                    color: Colors.white,
                    height: 30,
                  ),
                  const SizedBox(
                    height: 8,
                  ),
                  GestureDetector(
                    child: Container(
                      width: double.infinity,
                      height: 55,
                      alignment: Alignment.center,
                      color: Colors.white,
                      child: const Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Icon(
                            Icons.chat_bubble_outline,
                            color: Color.fromRGBO(87, 107, 149, 1),
                            size: 18,
                          ),
                          SizedBox(
                            width: 5,
                          ),
                          Text(
                            "发消息",
                            style: TextStyle(
                                fontSize: 16,
                                color: Color.fromRGBO(87, 107, 149, 1)),
                          )
                        ],
                      ),
                    ),
                    onTap: () async {
                      if (pageParams.prePageType == MobileChatPage) {
                        RouteUtils.pop();
                        return;
                      }
                      await RouteUtils.route2MobileChatPage(
                          MobileChatPageParams(
                              contactUserId: contactsUserInfo.id,
                              contactUserCode: contactsUserInfo.code,
                              contactUserNickname: contactsUserInfo.nickname!,
                              contactUserHeadPortrait:
                                  contactsUserInfo.headPortrait,
                              contactUserHeadPortraitStoreType:
                                  contactsUserInfo.headPortraitStoreType,
                              currentUserId: appUserDetail.id!,
                              currentUserCode: appUserDetail.code,
                              currentUserNickName: appUserDetail.nickname,
                              currentUserHeadPortrait:
                                  appUserDetail.headPortrait,
                              currentUserHeadPortraitStoreType:
                                  appUserDetail.headPortraitStoreType,
                              localConversation: pageParams.localConversation));
                    },
                  )
                ],
              ),
            ),
          );
        },
      );
    });
  }
}
